package com.tianqu.tms.www.transfer.controller;

import java.util.List;

import com.tianqu.tms.core.base.UserContext;
import com.tianqu.tms.core.util.TmsConstants;
import com.tianqu.tms.entity.TransferAcceptedInfo;
import com.tianqu.tms.www.member.action.MemberUDTopListAction;
import com.tianqu.tms.www.transfer.viewhelper.TransferViewHelper;

/**
 *
 * @author Jiazheng Sun
 */
public class TransferListController extends AbstractTransferController {
	//
	private String vin;
	private String serial;
	private String action;
	private String searchType;
	private String licensePlate;
	public static final String PAGING = "paging";
	public static final String ATTNLIST = "attnList";
	
	@Override
	public void doExecute(UserContext context) throws Exception {
		TransferAcceptedInfo processingTransferAccepted = new TransferAcceptedInfo();
		TransferAcceptedInfo auditingTransferAccepted = new TransferAcceptedInfo();
		TransferAcceptedInfo allTransferAccepted = new TransferAcceptedInfo();
		if (context.getAttribute(MemberUDTopListAction.PAGENO) != null) {
			processingTransferAccepted.setPageNo(Integer.parseInt((String) context.getAttribute(MemberUDTopListAction.PAGENO)));
			auditingTransferAccepted.setPageNo(Integer.parseInt((String) context.getAttribute(MemberUDTopListAction.PAGENO)));
		}
		if (context.getAttribute(MemberUDTopListAction.MAXINDEX) != null) {
			processingTransferAccepted.setMaxIndex(Integer.parseInt((String) context.getAttribute(MemberUDTopListAction.MAXINDEX)));
			auditingTransferAccepted.setMaxIndex(Integer.parseInt((String) context.getAttribute(MemberUDTopListAction.MAXINDEX)));
		}
		if (context.getAttribute(MemberUDTopListAction.SORTKEY) != null) {
			processingTransferAccepted.setSortKey(Integer.parseInt((String) context.getAttribute(MemberUDTopListAction.SORTKEY)));
			auditingTransferAccepted.setSortKey(Integer.parseInt((String) context.getAttribute(MemberUDTopListAction.SORTKEY)));
		}
		if (context.getAttribute(MemberUDTopListAction.ASC) != null) {
			processingTransferAccepted.setAsc((new Boolean((String) context.getAttribute(MemberUDTopListAction.ASC))).booleanValue());
			auditingTransferAccepted.setAsc((new Boolean((String) context.getAttribute(MemberUDTopListAction.ASC))).booleanValue());
		}
		
		//
		final TransferViewHelper helper = new TransferViewHelper();
		final String loginId = context.getUserProfile().getLoginId();
		final boolean isOperator = context.getUserProfile().getUserType() == 1; //Operator Type
		
		final List<TransferAcceptedInfo> auditing = isOperator ? auditingTransferAccepted.getAuditingTransfers() : auditingTransferAccepted.getAuditingTransfers(loginId);
		int auditingTransferstotals = auditingTransferAccepted.getTotalCount();
		
		final List<TransferAcceptedInfo> processing = isOperator ? processingTransferAccepted.getProcessingTransfers() : processingTransferAccepted.getProcessingTransfers(loginId);
		int processingtotals = processingTransferAccepted.getTotalCount();
		
		final List<TransferAcceptedInfo> all = isOperator ? allTransferAccepted.getAllTransfers(null, null, null) : allTransferAccepted.getAllTransfers(null, null, null, loginId);
		int allTransfers = allTransferAccepted.getTotalCount();
		
		if (searchType == null) {
			helper.setTransfers(processing);
			helper.setTotalCount(processingtotals+"");
			context.setAttribute(PAGING, processingTransferAccepted); 
		} else if ("auditing".equalsIgnoreCase(searchType)) {
			helper.setTransfers(auditing);
			helper.setTotalCount(auditingTransferstotals+"");
			context.setAttribute(PAGING, auditingTransferAccepted); 
		} else if ("all".equalsIgnoreCase(searchType)) {
			if (action != null) {
				final List<TransferAcceptedInfo> transfers = isOperator ? processingTransferAccepted.getAllTransfers(serial, vin, licensePlate) : processingTransferAccepted.getAllTransfers(serial, vin, licensePlate, loginId);
				helper.setTransfers(transfers);
			}
		}
	
		helper.setSearchType(searchType);
		helper.setAllCount(allTransfers+"");
		helper.setAuditingCount(auditingTransferstotals+"");
		helper.setProcessingCount(processingtotals+"");
		context.setAttribute(TmsConstants.VIEWHELP, helper);
	}


	/*
	 * 
	 */
	
	public String getSearchType() {
		return searchType;
	}
	
	public String getVin() {
		return vin;
	}

	public void setVin(String vin) {
		this.vin = vin;
	}

	public String getSerial() {
		return serial;
	}

	public void setSerial(String serial) {
		this.serial = serial;
	}

	public String getLicensePlate() {
		return licensePlate;
	}
	
	public void setLicensePlate(String licensePlate) {
		this.licensePlate = licensePlate;
	}
	
	public void setSearchType(String searchType) {
		this.searchType = searchType;
	}

	public String getAction() {
		return action;
	}

	public void setAction(String action) {
		this.action = action;
	}

}
